package com.liveneo.adapter.mobile.web.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.dto.LoreInfoDto;
import com.liveneo.adapter.mobile.model.request.LoreInfoRequest;
import com.liveneo.adapter.mobile.model.request.LoreListRequest;
import com.liveneo.adapter.mobile.model.response.LoreInfoResponse;
import com.liveneo.adapter.mobile.model.response.LoreListResponse;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;

/**
  * @Description: 知识库信息相关接口
  * @return TODO
  * @author liveneo-shuqw
  * @date 2016年6月15日 上午9:47:50
  * Copyright: Copyright (c) 2015 
  * Company:北京朗泰
  * @version 3.0
  */
@Controller
@RequestMapping("/mobile")
public class MobileLoreController extends BaseController {
    /**
      * @Title: 获取知识库列表接口
      * @Description: TODO
      * @param @param data
      * @param @return 设定文件
      * @return String 返回类型
      * @throws
     */
    @SuppressWarnings("unused")
    @RequestMapping("/getLoreList.action")
    @ResponseBody
    public String getLoreList(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getLoreList.action入参=" + loguuid;
        String result = null;
        LoreListResponse response = null;
        try {
            LoreListRequest lr = JsonUtils.fromJsonWithBase64(logTitle, data, LoreListRequest.class);
            List<LoreInfoDto> loreInfoList = new ArrayList<LoreInfoDto>();
            // TODO 待查询 待完成
            loreInfoList.addAll(createData());
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE,
                    MobileReturnCodeSupport.CODE_PARAM_NO_UUID, LoreListResponse.class);
            response.setLoreInfoList(loreInfoList);
        } catch (Exception e) {
            LogUtils.error("获取知识库列表出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LoreListResponse.class);
        }
        result = JsonUtils.toJson(response);
        LogUtils.info("获取知识库列表返回结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }

    private List<LoreInfoDto> createData() {
        List<LoreInfoDto> loreInfoList = new ArrayList<LoreInfoDto>();
        LoreInfoDto dto = new LoreInfoDto();
        dto.setLoreTitle("测试知识库标题1");
        dto.setLoreUuid("123456789");
        loreInfoList.add(dto);
        return loreInfoList;
    }

    /**
      * @Title: 查勘端-获取知识信息
      * @Description: TODO
      * @param @param data
      * @param @return 设定文件
      * @return String 返回类型
      * @throws
     */
    @SuppressWarnings("unused")
    @RequestMapping("/getLoreInfo.action")
    @ResponseBody
    public String getLoreInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getLoreInfo.action入参=" + loguuid;
        String result = null;
        LoreInfoResponse response = null;
        try {
            LoreInfoRequest lr = JsonUtils.fromJsonWithBase64(logTitle, data, LoreInfoRequest.class);
            // TODO 待完成
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE,
                    MobileReturnCodeSupport.CODE_PARAM_NO_UUID, LoreInfoResponse.class);
            response.setKbinfo("测试信息");
        } catch (Exception e) {
            LogUtils.error("获取知识库信息出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LoreInfoResponse.class);
        }
        result = JsonUtils.toJson(response);
        LogUtils.info("获取知识库信息返回结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }
}
